﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class Admin_ucControl_ucSize : System.Web.UI.UserControl
{
    TblCategoryDAO cateDAO = new TblCategoryDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCate();
            string id = Request.QueryString["id"];
            if (id !=null)
            {
                LoadDetail();
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;    
        try
        {
            TblCategory cate = new TblCategory();
            cate.CategoryName = txtCateName.Text.Trim();
           
            string id = Request.QueryString["id"];
            if (id == null || id == "")
            {             
                cateDAO.Category_InsUp(cate, 1);
            }
            else
            {
                cate.IDCategory = Global.ConvertToInt(id);
                cateDAO.Category_InsUp(cate, 2);
            }
            flag = true;

        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            LoadCate();
            txtCateName.Text = string.Empty;
            Response.Redirect("~/Admin/Category.aspx");
        }
    }

    private void LoadCate()
    {
        grvCategory.DataSource = cateDAO.GetCate();
        grvCategory.DataBind();
    }

    private void LoadDetail()
    {
        string id = Request.QueryString["id"];
        TblCategory cate = cateDAO.GetCateByIDCate(Global.ConvertToInt(id));
        if (cate != null)
        {
            txtCateName.Text = cate.CategoryName;          
        }
    }
}
